package com.zygame.sdk.view.web;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;

import com.zygame.sdk.ConstantValue;
import com.zygame.sdk.bean.Toolbar;
import com.zygame.sdk.bean.ZY_Toolbar;
import com.zygame.sdk.model.UserModel;
import com.zygame.sdk.utils.http.HttpUtil;
import com.zygame.sdk.utils.http.NetHttpUtil.DataCallback;
import com.zygame.sdk.utils.ui.UiManager;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class ZY_AccountCenterWeb extends BaseActivity implements OnClickListener {
	private UiManager manager;

	// private MoreView moreView;

	// private String TAG = "65SDK";
	private ArrayList<Toolbar> toolbars;

	private HashMap<String, FYToolBarWebView> VIEWCACHE = new HashMap<String, FYToolBarWebView>();// key
	private FYToolBarWebView curredView;

	private Toolbar currentToolbar;

	String lastKey = "";

	@Override
	public void onClick(View v) {
		ll_top_middle.removeAllViews();
		currentToolbar = (Toolbar) v.getTag();
//		String url = currentToolbar.url;

		String key = currentToolbar.action;
		if (key.equals(lastKey))
			return;
		if (VIEWCACHE.containsKey(key)) {
			curredView = VIEWCACHE.get(key);
		} else {
			FYToolBarWebView view = new FYToolBarWebView(this, currentToolbar);
			VIEWCACHE.put(key, view);
			curredView = view;
		}
		manager.changeViewNoHistroy(key, curredView);
		lastKey = key;
	}

	DataCallback<JSONObject> dataCallback = new DataCallback<JSONObject>() {
		@Override
		public void callbackSuccess(JSONObject paramObject) {
			// TODO Auto-generated method stub
			JSONObject object = paramObject.optJSONObject("data");
			UserModel.getInstance().setPursecoin(object.optInt("balance") + "");
		}

		@Override
		public void callbackError(String error) {
			// TODO Auto-generated method stub
		}
	};

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		if (lastKey.equals("more")) {
			HttpUtil.getBalance(this, dataCallback);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void goBack() {
		if (currentToolbar == null) {
			if (curredView != null) {
				if (!curredView.goBack())
					finish();
			} else {
				finish();
			}
		} else {
			if (currentToolbar.type.equals("1")) {
				finish();
			} else {
				FYToolBarWebView fyToolBarWebView = VIEWCACHE.get(currentToolbar.action);
				if (fyToolBarWebView != null && !fyToolBarWebView.goBack()) {
					finish();
				}
			}
		}
	}

	@Override
	protected void loadViewLayout() {
		ZY_Toolbar main = getIntent().getParcelableExtra(ConstantValue.TOOLBARFLAG);
		if (main == null)
			return;
		manager = new UiManager();
		manager.setShowContiner(ll_content);
		btn_nav_top_right.setVisibility(View.GONE);
		rl_base_topBar.setVisibility(View.GONE);
		hideTopBar();
//		if (main.toolbar.equals("0")) {
//			hideBottom();
//			curredView = new FYToolBarWebView(this, main);
//			manager.changeView(curredView);
//		} else {
//			showBottom();
//			toolbars = (ArrayList<Toolbar>) ConfigMessage.getInstance().getToolbarData();
//			int toolBarSize = toolbars == null ? 0 : toolbars.size();
//			for (int i = 0; i < toolBarSize; i++) {
//				Toolbar toolbar = toolbars.get(i);
//				BottomItem item = new BottomItem(this, toolbar);
//				item.setOnClickListener(this);
//				item.setTag(toolbar);
//				rg_base_bottom.addView(item);
//				if (toolbar.name.equals(main.name)) {
//					item.performClick();
//					currentToolbar = toolbar;
//				}
//			}
//		}
	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void leftClick() {
		goBack();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		// if (requestCode == MyWebView.FILECHOOSER_RESULTCODE) {
		// if (null == MyWebView.mUploadMessage)
		// return;
		// Uri result = intent == null || resultCode != RESULT_OK ? null :
		// intent.getData();
		// MyWebView.mUploadMessage.onReceiveValue(result);
		// MyWebView.mUploadMessage = null;
		// }
	}
}
